package com.tim.helper.inactive.remodule;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 @author Adminis
 @date 2020年8月4日
 DESC:
 */
@Slf4j
public class EntityAddSwagger {
	String params = "@ApiImplicitParams({\nPARAMS\n})";
	String paramTemplate = "\t@ApiImplicitParam(dataType = \"TYPE\", name = \"NAME\", value = \"VALUE\", paramType = \"query\", required = false)";

	/**根据dto生成@ApiImplicitParams*/
	@Test public void genApiImplicitParams() throws IOException {
		String file =
				"AddResponsibilityRemindDTO.java";

		List<String> infos =
				Files.readAllLines(Paths.get("C:/Users/Adminis/Desktop/start/dto", file),
						StandardCharsets.UTF_8);
		// infos.forEach(System.out::println);
		System.out.println("@ApiImplicitParams({"); StringBuffer buf = new
				StringBuffer(); int state = 0; for (int i = 0; i < infos.size(); i++) {
			String info = infos.get(i); if(info.trim().startsWith("@ApiModelProperty")) {
				i = genInfo(infos, buf, i); } if(state==0 &&
					info.trim().startsWith("public class")) {
				if(info.contains("extends PageBean")) { state = 1; }else
				if(info.contains("extends BaseAreaStoreIdsPageDTO")){ state=2; }else
				if(info.contains("extends ConfigBaseDTO")){ state=3; }else { state=-1; }

			}

		} System.out.println(buf.substring(0, buf.length()-(state>0?1:2)));
		if(state==2) {
			// printApiImplicitParam("BaseAreaStoreIdsPageDTO"); }
			if(state==3) {
				printApiImplicitParam("ConfigBaseDTO");
			}
			if(state>0) {
				printApiImplicitParam("PageBean");
			}
			System.out.println("})");
		}
	}

	//按dto构建ApiImplicitParam
	@Test
	public void printApiImplicitParam(String file) throws IOException { // String
		file = "PageBean.java";
		file = file+".java";
		List<String> infos =
				Files.readAllLines(Paths.get("C:/Users/Adminis/Desktop/start/dto", file),
						StandardCharsets.UTF_8);
		StringBuffer buf = new StringBuffer();
		System.out.println(buf.substring(0,
				buf.length()-("PageBean.java".equals(file)?2:1)));
	}
	private int genInfo(List<String> infos, StringBuffer buf, int i) {
		String info = infos.get(i);
		if(info.trim().startsWith("@ApiModelProperty")) {
			int lastQuote = info.lastIndexOf("\"");
			if(lastQuote==-1) return i;
			int firstQuote = info.indexOf("\"")+1;
			String value, type, name;
			value = info.substring(firstQuote, lastQuote);
			while(!info.contains("private ")) {
				info = infos.get(++i);//无private，则非变量声明行。
			}
			int typeStart = info.indexOf("private")+8;
			int typeEnd = info.indexOf(" ", typeStart);
			int nameEnd = info.indexOf("=", typeEnd)>-1 ? info.indexOf("=", typeEnd) : info.indexOf(";");
			type = info.substring(typeStart, typeEnd);
			name = info.substring(typeEnd, nameEnd);
			String param = paramTemplate.replace("TYPE", type.trim())
					.replace("NAME", name.trim())
					.replace("VALUE", value.trim());
			buf.append(param).append(",\n");
		}
		return i;
	}

	/**dto添加@ApiModelProperty*/
	String annoTemplate = "@ApiModelProperty(value = \"VALUE\")\n\t";
	@Test
	public void addApiModelProperty() throws Exception {
		String file = "";

		List<String> infos = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
		infos.add(2, "import io.swagger.annotations.ApiModel;");
		infos.add(3, "import io.swagger.annotations.ApiModelProperty;");
		for (int i = 0; i < infos.size(); i++) {
			String info = infos.get(i);
			if(info.trim().startsWith("public class")) {
				info = "@ApiModel(description=\"\")\n"+info;
			}
			if(info.trim().startsWith("private ") && !info.trim().startsWith("private static") ) {
				//private前拼接注解
				String preLineVal = infos.get(i-1).replace("/**", "").replace("*/", "").trim();
				if(preLineVal.startsWith("@JsonFormat")) {
					preLineVal = infos.get(i-2).replace("/**", "").replace("*/", "").trim();
				}else if(preLineVal.startsWith("@")) {
					throw new Exception();
				}
				String anno = annoTemplate.replace("VALUE", preLineVal);
				info = info.replace("private", anno+"private");
			}
			System.out.println(info);
		}
	}
	@Test
	public void addApiModelProperty2() throws Exception {
		String file = "";

		List<String> infos = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
		infos.add(2, "import io.swagger.annotations.ApiModel;");
		infos.add(3, "import io.swagger.annotations.ApiModelProperty;");
		String annoContent = "";
		for (int i = 0; i < infos.size(); i++) {
			String info = infos.get(i);
			String tempStr = info.trim();
			if(tempStr.startsWith("public class")) {
				info = "@ApiModel(description=\"\")\n"+info;
				annoContent = "";
			}else if(tempStr.startsWith("private ")) {
				if(!tempStr.startsWith("private static")) {
					//private前拼接注解
					String anno = annoTemplate.replace("VALUE", annoContent);
					info = info.replace("private", anno+"private");
				}
				annoContent = "";
			}else if(tempStr.startsWith("/**")) {
				if(tempStr.endsWith("*/")) {
					annoContent = tempStr.replace("/**", "").replace("*/", "").trim();
				}else {
					while(!tempStr.endsWith("*/")) {
						annoContent += tempStr.replace("/**", "").replaceFirst("\\* ", " ").trim();
						String line = infos.get(++i);
						info += "\n"+line;
						tempStr = line.trim();
					}
					annoContent += tempStr.replace("*/", " ").trim();
				}
			}else if(tempStr.startsWith("//")) {
				annoContent += tempStr.replace("//", "").trim();
			}
			System.out.println(info);
		}
	}

	/**
	 遍历，按行处理
	 @throws Exception
	 @author Litianlin
	 @date 2020年9月30日
	 */
	@Test
	public void addApiModelProperty3() throws Exception {
		String file = "";

		List<String> infos = Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
		infos.add(2, "import io.swagger.annotations.ApiModel;");
		infos.add(3, "import io.swagger.annotations.ApiModelProperty;");
		String annoContent = "";
		int modelFlag = 0;//类@ApiModel标记
		int propertyFlag = 0;//方法@ApiModelProperty标记
		for (int i = 0; i < infos.size(); i++) {
			String info = infos.get(i);
			String tempStr = info.trim();
			if(tempStr.startsWith("public class")) {
				if(modelFlag == 0) {
					info = "@ApiModel(description=\""+annoContent+"\")\n"+info;
				}
				annoContent = "";
			}else if(tempStr.startsWith("@ApiModelProperty")) {
				propertyFlag = 1;//已有@ApiModelProperty
			}else if(tempStr.startsWith("@ApiModel")) {
				modelFlag = 1;//已有@ApiModel
			}else if(tempStr.startsWith("private ")) {
				if(!tempStr.startsWith("private static") && propertyFlag!=1) {
					//private前拼接注解
					String anno = annoTemplate.replace("VALUE", annoContent);
					info = info.replace("private ", anno+"private ");
				}
				if(info.contains("_")) {
					info = subLine2UpperCase(info);
				}
				annoContent = "";
				propertyFlag = 0;
			}else if(tempStr.startsWith("/**")) {
				if(tempStr.endsWith("*/")) {
					annoContent = tempStr.replace("/**", "").replace("*/", "").trim();
				}else {
					while(!tempStr.endsWith("*/")) {
						if(!tempStr.matches("\\*\\s*\\@[\\w\\W]+") && !"*".equals(tempStr)) {
							annoContent += tempStr.replace("/**", "").replaceFirst("\\* ", " ").trim();
						}
						String line = infos.get(++i);
						info += "\n"+line;
						tempStr = line.trim();
					}
					annoContent += tempStr.replace("*/", " ").trim();
				}
			}else if(tempStr.startsWith("//")) {
				annoContent += tempStr.replace("//", "").trim();
			}
			System.out.println(info);
		}
	}

	private String subLine2UpperCase(String str) {
		String regex = "_[a-z]";
		Matcher matcher = Pattern.compile(regex).matcher(str);
		while(matcher.find()) {
			String group = matcher.group();
			str = str.replace(group, group.toUpperCase().substring(1));
		}
		return str;
	}
	public static void main(String[] args) throws Exception {
//		new ParamHelper().genApiImplicitParams();
//		new ParamHelper().addApiModelProperty()();
		new EntityAddSwagger().addApiModelProperty2();

	}
}
